﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.Entity;
using JustMVCTest.Models;
using Telerik.Web.Mvc;
using System.Text;
using System.IO;
using System.Collections;
using Telerik.Web.Mvc.Extensions;
using System.Diagnostics;

namespace JustMVCTest.Controllers
{
    public class HistoryController : Controller
    {
        //
        // GET: /History/
        private PODBContext db2 = new PODBContext();

        public ActionResult HistoryPOsExportTableToCsv(int Item, long PONumber)
        {

            MemoryStream output = new MemoryStream();
            StreamWriter writer = new StreamWriter(output, Encoding.UTF8);

                writer.Write("po_number,");
                writer.Write("item,");
                writer.Write("status,");
                writer.Write("description,");
                writer.Write("delivery_date,");
                writer.Write("requisitioner,");
                writer.Write("delivery_date_request,");
                writer.Write("cost_center,");
                writer.Write("internal_order,");
                writer.Write("prof_center,");
                writer.Write("percentage,");
                writer.Write("cost_element,");
                writer.Write("cost_element_name,");
                writer.Write("vendor,");
                writer.Write("vendor_name,");
                writer.Write("um,");
                writer.Write("T_currency,");
                writer.Write("sch_lin,");
                writer.Write("order_quantity,");
                writer.Write("open_quantity,");
                writer.Write("open_amount_T,");
                writer.Write("order_amount,");
                writer.Write("open_amount_L,");
                writer.Write("TSSC_Pending_Send_Date,");
                writer.Write("Purchase_O");
                writer.WriteLine();

                var pos = from m in db2.POs
                          select m;

                        pos = pos.Where(s => s.Item == Item);
                        pos = pos.Where(s => s.PONumber == PONumber);
                        pos = pos.OrderByDescending(s => s.Update_Num);

                foreach (PO po in pos) {

                    writer.Write(po.PONumber); writer.Write(",");
                    writer.Write(po.Item); writer.Write(",");
                        writer.Write(po.Status); writer.Write(",");
                   
                        String Descr_tmp = po.Descr.Replace(",", " ");
                        writer.Write(Descr_tmp); writer.Write(",");
                    
                        writer.Write(po.DeliveryDate); writer.Write(",");
                   
                        writer.Write(po.Requisitioner); writer.Write(",");
                   
                        writer.Write(po.DeliveryDate_Req); writer.Write(",");
                    
                        writer.Write(po.Cost_Cent); writer.Write(",");
                   
                        writer.Write(po.Int_Order); writer.Write(",");
                   
                        writer.Write(po.Prof_Cent); writer.Write(",");
                   
                        writer.Write(po.Percentage); writer.Write(",");
                    
                        writer.Write(po.cost_elem); writer.Write(",");

                        writer.Write(po.co_el_name); writer.Write(",");
                   
                        writer.Write(po.Vendor); writer.Write(",");
                   
                        String VendorName_tmp = po.Descr.Replace(",", " ");
                        writer.Write(VendorName_tmp); writer.Write(",");
                  
                        writer.Write(po.UM); writer.Write(",");
                  
                        writer.Write(po.T_Curr); writer.Write(",");
                   
                        writer.Write(po.Sch_Lin); writer.Write(",");
                 
                        writer.Write(po.Ord_Qty); writer.Write(",");
                    
                        writer.Write(po.Open_Qty); writer.Write(",");
                   
                        writer.Write(po.Open_Amt_T); writer.Write(",");
                  
                        writer.Write(po.Order_Amt); writer.Write(",");

                        writer.Write(po.Open_Amt_L); writer.Write(",");
                    
                        writer.Write(po.TSSC_Pending_Send_Date); writer.Write(",");

                        writer.Write(po.Purchase_O); 
                    
                    writer.WriteLine();

                    
                }
                writer.Flush();
                output.Position = 0;
            return File(output, "text/comma-separated-values", "History_POs.csv"); 
        }

        public ActionResult Index(int Item, long PONumber)
        {
            var pos = from m in db2.POs
                      select m;
            if (pos == null)
            {
                return HttpNotFound();
            }

            pos = pos.Where(s => s.Item == Item);
            pos = pos.Where(s => s.PONumber == PONumber);
            pos = pos.OrderByDescending(s => s.Update_Num);


            return View(pos);
        }

    }
}
